﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PhysicalTagBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPhysicalRequestDA PhysicalRequestDA = DataAccess.CreatePhysicalRequestDA();
        private static readonly IPhysicalTagDA PhysicalTagDA = DataAccess.CreatePhysicalTagDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();
        private static readonly ITaskDetailDA taskDetailDA = DataAccess.CreateTaskDetailDA();

        public PhysicalTagBLL()
        {
            this.pubParasInfo = null;
        }

        public PhysicalTagBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PhysicalTagBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeletePhysicalTag(List<string> IDS, List<string> TagNo)
        {
            string temp = string.Empty;
            try
            {
                int i;
                List<PhysicalTagInfo> PhysicalTagInfos = new List<PhysicalTagInfo>();
                PhysicalTagInfo physicalTagInfo = null;
                for (i = 0; IDS.Count > i; i++)
                {
                    physicalTagInfo = PhysicalTagDA.GetPhysicalTagByID(IDS[i], Convert.ToInt32(TagNo[i]), this.dataBaseID[0], this.pubParasInfo);
                    PhysicalTagInfos.Add(physicalTagInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (PhysicalTagDA.DeletePhysicalTag(IDS, TagNo, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PhysicalTagInfo>(PhysicalTagInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PHYSICAL_REQUEST", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    return temp.Substring(0, temp.Length - 1);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return temp;
        }

        public DataSet GetAllPhysicalTag()
        {
            return PhysicalTagDA.GetAllPhysicalTag(this.dataBaseID[0], this.pubParasInfo);
        }

        public string GetPhysicalRequestStatus(DataBase dataBase, DbTransaction tran, string PhysicalRequestID, string Status)
        {
            try
            {
                int i = PhysicalTagDA.GetPhysicalRequestStatus(dataBase, tran, PhysicalRequestID, Status, this.dataBaseID[0]);
                string CS40001 = Status.ToUpper();
                if (CS40001 == null)
                {
                    goto Label_0093;
                }
                if (!(CS40001 == "COUNTED"))
                {
                    if (CS40001 == "RECOUNTED")
                    {
                        goto Label_0064;
                    }
                    goto Label_0093;
                }
                if (i > 0)
                {
                    return PhysicalRequestStatus.InCounting;
                }
                return PhysicalRequestStatus.Counted;
            Label_0064:
                if (i > 0)
                {
                    return PhysicalRequestStatus.InRecounting;
                }
                return PhysicalRequestStatus.Recounted;
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
        Label_0093:
            return "";
        }

        public DataSet GetPhysicalTagByQueryEntity(PhysicalTagQueryEntity RequestEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(RequestEntity.PhysicalRequestID))
            {
                sqlWhere.Add(" PT.PHYSICAL_REQUEST_ID " + RequestEntity.PhysicalRequestID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.TagNo))
            {
                sqlWhere.Add(" PT.TAG_NO " + RequestEntity.TagNo);
            }
            if (!string.IsNullOrEmpty(RequestEntity.TeamNo))
            {
                sqlWhere.Add(" PT.TEAM_NO " + RequestEntity.TeamNo);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerID))
            {
                sqlWhere.Add(" PT.OWNER_ID " + RequestEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuID))
            {
                sqlWhere.Add(" PT.SKU_ID " + RequestEntity.SkuID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.Loc))
            {
                sqlWhere.Add(" PT.Loc " + RequestEntity.Loc);
            }
            if (!string.IsNullOrEmpty(RequestEntity.Lot))
            {
                sqlWhere.Add(" PT.Lot " + RequestEntity.Lot);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LpnID))
            {
                sqlWhere.Add(" PT.Lpn_ID " + RequestEntity.LpnID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.CountQty))
            {
                sqlWhere.Add(" PT.Count_Qty " + RequestEntity.CountQty);
            }
            if (!string.IsNullOrEmpty(RequestEntity.TaskDetailID))
            {
                sqlWhere.Add("TDI.Task_Detail_ID " + RequestEntity.TaskDetailID);
            }
            DataSet ds = PhysicalTagDA.GetPhysicalTagByQueryList(sqlWhere, RequestEntity, this.dataBaseID[0], this.pubParasInfo);
            bool setQty = false;
            SysSettingInfo sysSettingInfo = sysSettingDA.GetSysSettingByID("PCSetCountQtySameAsSysQty", this.dataBaseID[0], this.pubParasInfo);
            if (sysSettingInfo != null)
            {
                if (!string.IsNullOrEmpty(sysSettingInfo.Value))
                {
                    if (sysSettingInfo.Value == "Y")
                    {
                        setQty = true;
                    }
                }
                else if (sysSettingInfo.ValueDefault == "Y")
                {
                    setQty = true;
                }
            }
            if (((ds != null) && (ds.Tables.Count > 2)) && (ds.Tables[2].Rows.Count > 0))
            {
                for (int i = 0; ds.Tables[2].Rows.Count > i; i++)
                {
                    if (setQty && (decimal.Parse(ds.Tables[2].Rows[i]["COUNT_QTY"].ToString()) == 0M))
                    {
                        ds.Tables[2].Rows[i]["COUNT_QTY"] = ds.Tables[2].Rows[i]["SYS_QTY"];
                    }
                }
            }
            return ds;
        }

        public PhysicalTagInfo GetPhysicalTagInfoByID(string id, int TagNo)
        {
            return PhysicalTagDA.GetPhysicalTagByID(id, TagNo, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<string> GetPhysicalTagNoListByID(string physicaltagID)
        {
            return PhysicalTagDA.GetPhysicalTagNoListByID(physicaltagID, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertPhysicalTag(PhysicalTagInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (PhysicalTagDA.InsertPhysicalTag(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    PhysicalRequest = obj.PhysicalRequestID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return PhysicalRequest;
        }

        public string UpdatePhysicalTag(PhysicalTagInfo obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                if (PhysicalTagDA.UpdatePhysicalTag(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    PhysicalRequest = obj.TagNo.ToString();
                }
                PhysicalRequestDA.UpdatePhysicalRequestStatus(dataBase, tran, obj.PhysicalRequestID, this.GetPhysicalRequestStatus(dataBase, tran, obj.PhysicalRequestID, "COUNTED"), this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return PhysicalRequest;
        }

        public string UpdatePhysicalTagList(List<PhysicalTagInfo> obj, string CountType)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                for (int i = 0; obj.Count > i; i++)
                {
                    PhysicalTagInfo oldPhysicalTagInfo = PhysicalTagDA.GetPhysicalTagByID(dataBase, tran, obj[i].PhysicalRequestID, obj[i].TagNo, this.dataBaseID[0], this.pubParasInfo);
                    if ((CountType.ToUpper() == "COUNT") && (PhysicalTagDA.UpdatePhysicalTagByCount(dataBase, tran, obj[i], this.dataBaseID[0]) > 0))
                    {
                        PhysicalRequest = PhysicalRequest + obj[i].TagNo.ToString() + ",";
                        AuditDataBLL auditDataBLL = new AuditDataBLL();
                        auditDataBLL.InsertAuditData<PhysicalTagInfo>(dataBase, tran, oldPhysicalTagInfo, obj[i], AuditAction.Update, this.pubParasInfo.UserID, "PHYSICAL_TAG", this.dataBaseID[0]);
                        taskDetailDA.UpdateTaskDetailStatusByPc(dataBase, tran, obj[i].PhysicalRequestID, obj[i].TagNo.ToString(), TaskStatus.Completed, TaskType.PC, this.pubParasInfo.UserID, this.dataBaseID[0]);
                    }
                    if ((CountType.ToUpper() == "RECOUNT") && (PhysicalTagDA.UpdatePhysicalTagByReCount(dataBase, tran, obj[i], this.dataBaseID[0]) > 0))
                    {
                        PhysicalRequest = PhysicalRequest + obj[i].TagNo.ToString() + ",";
                        new AuditDataBLL().InsertAuditData<PhysicalTagInfo>(dataBase, tran, oldPhysicalTagInfo, obj[i], AuditAction.Update, this.pubParasInfo.UserID, "PHYSICAL_TAG", this.dataBaseID[0]);
                        taskDetailDA.UpdateTaskDetailStatusByPc(dataBase, tran, obj[i].PhysicalRequestID, obj[i].TagNo.ToString(), TaskStatus.Completed, TaskType.PR, this.pubParasInfo.UserID, this.dataBaseID[0]);
                    }
                }
                PhysicalRequestDA.UpdatePhysicalRequestStatus(dataBase, tran, obj[0].PhysicalRequestID, this.GetPhysicalRequestStatus(dataBase, tran, obj[0].PhysicalRequestID, obj[0].Status), this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (!string.IsNullOrEmpty(PhysicalRequest))
            {
                PhysicalRequest = PhysicalRequest.Substring(0, PhysicalRequest.Length - 1);
            }
            return PhysicalRequest;
        }
    }
}

